<?php

namespace app\models\Child;

use Yii;

/**
 * This is the model class for table "sys_user_info".
 *
 * @property integer $user_id
 * @property string $user_name
 * @property string $user_phone
 * @property string $user_pwd
 * @property integer $user_gender
 * @property string $user_img
 * @property integer $user_level
 * @property integer $subclinic_id
 * @property integer $user_role
 * @property integer $user_status
 * @property string $leave_time
 * @property string $create_time
 *
 * @property SysRoleInfo $userRole
 * @property TSubclinicInfo $subclinic
 * @property InitGradeDoctor $userLevel
 * @property TReplyInfo[] $tReplyInfos
 */
class SysUserInfo extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'sys_user_info';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['user_gender', 'user_level', 'subclinic_id', 'user_role', 'user_status','user_title','work_year','level_type'], 'integer'],
            [['leave_time', 'create_time'], 'safe'],
            [['user_name', 'user_pwd'], 'string', 'max' => 50],
            [['user_phone'], 'string', 'max' => 11],
            [['user_img'], 'string', 'max' => 100],
            [['user_desc'], 'string', 'max' => 500],
            [['user_role'], 'exist', 'skipOnError' => true, 'targetClass' => SysRoleInfo::className(), 'targetAttribute' => ['user_role' => 'role_id']],
            [['subclinic_id'], 'exist', 'skipOnError' => true, 'targetClass' => TSubclinicInfo::className(), 'targetAttribute' => ['subclinic_id' => 'subclinic_id']],
            [['user_level'], 'exist', 'skipOnError' => true, 'targetClass' => InitGradeDoctor::className(), 'targetAttribute' => ['user_level' => 'grade_id']],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'user_id' => 'User ID',
            'user_name' => 'User Name',
            'user_phone' => 'User Phone',
            'user_pwd' => 'User Pwd',
            'user_gender' => 'User Gender',
            'user_img' => 'User Img',
            'user_level' => 'User Level',
            'subclinic_id' => 'Subclinic ID',
            'user_role' => 'User Role',
            'user_status' => 'User Status',
            'leave_time' => 'Leave Time',
            'create_time' => 'Create Time',
            'user_title' => 'User Title',
            'work_year' => 'Work Year',
            'level_type' => 'Level Type',
            'user_desc' => 'User Desc'
        ];
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getUserRole()
    {
        return $this->hasOne(SysRoleInfo::className(), ['role_id' => 'user_role']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getSubclinic()
    {
        return $this->hasOne(TSubclinicInfo::className(), ['subclinic_id' => 'subclinic_id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getUserLevel()
    {
        return $this->hasOne(InitGradeDoctor::className(), ['grade_id' => 'user_level']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getTReplyInfos()
    {
        return $this->hasMany(TReplyInfo::className(), ['doctor_id' => 'user_id']);
    }
    /**
     * @return \yii\db\ActiveQuery
     */
    public function getUserBySubclinicIdCount($subclinic_id)
    {
        return $list = $this->find()
            ->where(['subclinic_id' => $subclinic_id])
            ->asArray()
            ->count();
    }
    /**
     * @return \yii\db\ActiveQuery
     */
    public function getSysUserInfo($user_id)
    {
        $list = $this->find()
            ->where(['user_id' => $user_id])
            ->asArray()
            ->one();
        return $list;
    }
    /*
     * 根据分店ID获取医生列表
     */
    public function getDoctorList($subclinic_id)
    {
        if(!empty($subclinic_id))
        {
            $list = $this->find()
                    ->select('t_subclinic_info.*,sys_user_info.*')
                    ->join('left join', 't_subclinic_info','t_subclinic_info.subclinic_id = sys_user_info.subclinic_id')
                    ->where(['sys_user_info.subclinic_id' => $subclinic_id,'user_status' => 0])
                    ->asArray()
                    ->all();
        }else{
            $list = $this->find()
                    ->select('t_subclinic_info.*,sys_user_info.*')
                    ->join('left join', 't_subclinic_info','t_subclinic_info.subclinic_id = sys_user_info.subclinic_id')
                    ->where(['user_status' => 0])
                    ->asArray()
                    ->all();
        }
        
        return $list;
    }
}
